package com.wisdom.benefit.jufa.my;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.wisdom.benefit.jufa.main.DeviceInfoModel;
import com.wisdom.benefit.jufa.main.LoginActivity;
import com.wisdom.benefit.jufa.R;
import com.wisdom.benefit.jufa.base.BaseActivity;
import com.wisdom.benefit.jufa.base.BaseApplication;
import com.wisdom.benefit.jufa.main.MainActivity;
import com.wisdom.benefit.jufa.net.RetrofitService;
import com.wisdom.benefit.jufa.utils.CommonUtils;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class ChangePasswordActivity extends BaseActivity {
    private Button btn;
    private EditText oldEt, newEt;
    private RelativeLayout backRl;

    @Override
    protected int getLayout() {
        return R.layout.activity_change_password;
    }

    @Override
    protected void initViews() {
        //提交按钮
        btn = findViewById(R.id.change_password_btn);
        //旧密码
        oldEt = findViewById(R.id.change_password_old_et);
        //新密码
        newEt = findViewById(R.id.change_password_new_et);
        //返回按钮
        backRl = findViewById(R.id.change_password_back);
        setClick();
    }

    /**
     * 设置监听
     *
     * @return
     */
    private void setClick() {
        //提交按钮
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (TextUtils.isEmpty(oldEt.getText().toString())) {
                    Toast.makeText(ChangePasswordActivity.this, "请输入旧密码", Toast.LENGTH_SHORT).show();
                } else if (TextUtils.isEmpty(newEt.getText().toString())) {
                    Toast.makeText(ChangePasswordActivity.this, "请输入新密码", Toast.LENGTH_SHORT).show();
                } else {
                    changePsd(BaseApplication.getContext().getUserId(), oldEt.getText().toString(), newEt.getText().toString());
                }
            }
        });
        //返回按钮
        backRl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }


    @Override
    protected int getTitleStringRes() {
        return 0;
    }

    /**
     * 修改密码
     */
    private void changePsd(String userId, String oldPsd, String newPsd) {
        String deviceId = DeviceInfoModel.getDeviceId(getActivity());
        showProgressDialog("");
        addDisposable(RetrofitService.getNetService()
                .changePassword(userId, newPsd, oldPsd,deviceId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(response -> {
                    hideProgressDialog();
                    if (response.getCode() == 0) {
                        Toast.makeText(this, "修改密码成功", Toast.LENGTH_SHORT).show();
                        logout(BaseApplication.getContext().getUserId());
                    } else if (response.getCode() == -2){
                        showBindToast(response.getMsg());
                        logout(BaseApplication.getContext().getUserId());
                    }else{
                        showBindToast(response.getMsg());
                    }
                }, throwable -> {
                    throwable.printStackTrace();
                    hideProgressDialog();
                    if (CommonUtils.isNetworkAvailable(getContext()))
                        showBindToast("获取数据失败");
                    else
                        showBindToast("无网络连接，请稍后再试");
                }));
    }

    /**
     * 推出登陆
     */
    private void logout(String userId) {
        showProgressDialog("");
        addDisposable(RetrofitService.getNetService()
                .logout(userId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(response -> {
                    hideProgressDialog();
                    if (response.getCode() == 0) {
                        //步骤1：创建一个SharedPreferences对象
                        SharedPreferences sharedPreferences = getActivity().getSharedPreferences("data", Context.MODE_PRIVATE);
                        //步骤2： 实例化SharedPreferences.Editor对象
                        SharedPreferences.Editor editor = sharedPreferences.edit();
                        //步骤3：将获取过来的值放入文件
                        editor.putString("isHaveLogin", "0");
                        //步骤4：提交
                        editor.commit();
                        finish();
                        MainActivity.mainActivity.finish();
                        SettingActivity.instance.finish();
                        Intent intent = new Intent(getActivity(), LoginActivity.class);
                        startActivity(intent);
                    } else {
                        showBindToast(response.getMsg());
                    }
                }, throwable -> {
                    throwable.printStackTrace();
                    hideProgressDialog();
                    if (CommonUtils.isNetworkAvailable(getContext()))
                        showBindToast("退出失败");
                    else
                        showBindToast("无网络连接，请稍后再试");
                }));
    }
}
